<template>
	<div>
		<!-- 头部 -->
		<div class="header">
			<el-row style="padding: 5px 0 0 0;">
				<el-col :span="4">
					资质名称:
					<el-input v-model="aName" placeholder="请输入内容" style="width: 200px;"></el-input>
				</el-col>
				<el-col :span="4">
					资质id:
					<el-input v-model="aId" placeholder="请输入内容" style="width: 200px;"></el-input>
				</el-col>
				<el-col :span="2">
					<el-button size="mini" @click="selectLike"
						style="width: 90px;height: 40px;background-color: #00A0FF;color: #fff;">
						查询结果</el-button>
				</el-col>
				<el-col :span="2">
					<el-button size="mini" @click="select"
						style="width: 90px;height: 40px;background-color: #00A0FF;color: #fff;">
						获取资质</el-button>
				</el-col>
			</el-row>
		</div>
		<el-table :header-cell-style="{'text-align':'center'}" :cell-style="{ textAlign: 'center' }"
			:data="advertisersList.records" stripe style="width: 100% width: 80%;margin: auto">
			<el-table-column property="id" label="编号">
			</el-table-column>
			<el-table-column property="advCompanyName" label="资质名称">
			</el-table-column>
			<el-table-column property="abbreviation" label="简称">
			</el-table-column>
			<el-table-column label="是否启用">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.state" :active-value="0" :inactive-value="1" @change="handleSwitchChange(scope.row.id,$event)" ></el-switch>
				</template>
			</el-table-column>
			<el-table-column property="consumeSum" label="消耗">
			</el-table-column>
			<el-table-column property="projectNum" label="在投项目数">
			</el-table-column>
			<el-table-column property="conversionSum" label="转化数">
			</el-table-column>
			<el-table-column property="totalBalance" label="余额">
			</el-table-column>
			<el-table-column property="passRate" label="过审率">
				<template slot-scope="scope">
					<span>{{(scope.row.passRate*100).toFixed(2)}}%</span>
				</template>
			</el-table-column>
			<el-table-column label="平均转化">
				<template slot-scope="scope">
					<span>{{(scope.row.consumeSum/scope.row.conversionSum).toFixed(2)}}</span>
				</template>
			</el-table-column>
			<el-table-column label="标签">
				<template slot-scope="scope">
					<el-select v-model="scope.row.tagId" @change="handleTagChange(scope.row)">
						<el-option v-for="(item,index) in tags" v-bind:key="index" :label="item.name" :value="item.id">
						</el-option>
					</el-select>
				</template>
			</el-table-column>
			<el-table-column label="操作">
				<el-row slot-scope="scope">
					<el-col span="6"><el-button type="text" size="mini"
							@click=" viewAccount(scope.row.id,scope.row.advCompanyName)">查看账号</el-button></el-col>
				</el-row>	
			</el-table-column>
		</el-table>
		<div>
			<el-row style="margin: 20px 0;">
				<el-col :span="11">
					<div class="block">
						<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
							:current-page="currentPage4" :page-sizes="[10, 20, 50, 100]" :page-size="10"
							layout="total, sizes, prev, pager, next, jumper" :total="advertisersList.total">
						</el-pagination>
					</div>
				</el-col>
			</el-row>
		</div>
	</div>
</template>

<script>
	import '@/assets/css/index.css'
	import {
		Loading
	} from 'element-ui';
	import qs from 'qs'
	export default {
		name: "qu",
		data() {
			return {
				advertisersList: [],
				resourceCategory: [],
				aName: '',
				aId: '',
				role: '',
				categoryIds: '',
				dialogVisible: false,
				description: '',
				title: '',
				resource: [],
				roleDescriptions: {

				},
				tags: []
			}
		},
		created() {
			this.list()
		},
		methods: {
			list(){
				
				let than = this
				this.$axios.post('umsAdvertisingUserType/list', qs.stringify({
					size: 10,
					current: 1
				})).then((res) => {
					than.advertisersList = res.data;
					console.log(than.advertisersList)
				})
				this.$axios.post('tags/list').then((res) => {
					than.tags = res.data;
					console.log("tage:" + than.tags)
				})
			},
			handleSwitchChange(id,i){
				let loadingInstance = Loading.service(true);
				this.$axios.post('umsAdvertisingUserType/updateState/'+id+'/'+i).then((res) => {
					loadingInstance.close();
					if(res.data){
						this.$message({
							message: '修改成功',
							type: 'success'
						});
					}
				
				})
			},
			handleTagChange(row) {
				this.$axios.post('umsAdvertisingUserType/updateTag', qs.stringify({
					id: row.id,
					tagId: row.tagId
				})).then((res) => {
					// loadingInstance.close();
					if(res.data){
						this.$message({
							message: '修改成功',
							type: 'success'
						});
					}
				
				})
			},
			select() {
				this.$axios.post('umsAdvertisingUserType').then((res) => {
					console.log(res.data)
				})
			},
			viewAccount(id, name) {
				this.$router.push({
					name: 'advertisingUser',
					params: {
						accountId: id,
						name: name
					}
				});
			},
			handleSizeChange(val) {
				let that = this
				this.$axios.post('umsAdvertisingUserType/list', qs.stringify({
					size: val,
					current: 1,
					name: that.aName,
					id: that.aId
				})).then((res) => {
					that.advertisersList = res.data;
					console.log(that.advertisersList)
				})
			},
			handleCurrentChange(val) {
				let that = this;
				this.$axios.post('umsAdvertisingUserType/list', qs.stringify({
					size: that.advertisersList.size,
					current: val,
					name: that.aName,
					id: that.aId
				})).then((res) => {
					that.advertisersList = res.data;
				})
			},
			goToExternalUrl() {
				// 使用 window.location.href 跳转到外部 URL  
				window.location.href =
					'https://open.oceanengine.com/audit/oauth.html?app_id=1797546706202640&state=your_custom_params&scope=%5B300000000%2C2%2C4%2C5%2C9%2C130%2C110%2C112%2C14%2C120%2C122%2C123%2C124%2C100000005%5D&material_auth=1&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&rid=98ym2wdjps7'; // 外部 URL  
			},
			selectLike() {
				let that = this
				this.$axios.post('umsAdvertisingUserType/list', qs.stringify({
					size: 10,
					current: 1,
					name: that.aName,
					id: that.aId
				})).then((res) => {
					that.advertisersList = res.data;
					console.log(that.advertisersList)
				})
			},
		}
	}
</script>

<style>
	.header {
		width: auto;
		height: 50px;
		border: 1px solid #ddd;
		margin: 15px auto;
		border-radius: 3px 3px;
		position: relative;
	}
</style>